/*angular的公共文件*/

var navbarApp = angular.module('SJ-APP', []).config(['$interpolateProvider', function ($interpolateProvider) {
    $interpolateProvider.startSymbol('@{');
    $interpolateProvider.endSymbol('}@');
}]);
navbarApp.controller('initNavbar', function ($scope) {
    $scope.$on('finished', function (finishedEvent) {
        $(".profile .top").click(function(e){
            var el = $("#popup_settings");
            var topEl = $(".profile .top");
            var w = (topEl.width()-100)/2;//el.width()
            el.show();
            el.css({
                left : topEl.offset().left+w
            });
        });
    });

    $scope.arrayValue = [{
        href: '/home',
        iconClass: 'icon home',
        field: '首页',
        value: 'home'
    }, {
        href: '/popular',
        iconClass: 'icon discover',
        field: '发现',
        activeCls: 'active top',
        value: 'popular'
    }, {
        href: '#',
        iconClass: 'icon groups',
        field: '论坛',
        value: 'groups'
    }]
    for (var i = 0, obj; obj = $scope.arrayValue[i]; i++) {
        if (window.location.href.indexOf(obj.value) != -1) {
            obj.activeCls = 'active top';
        } else {
            obj.activeCls = 'top';
        }
    }
});
/*发现页begin*/
navbarApp.controller('photoController', function ($scope, $http) {
    var community = {
        url: "/popular/json/faxian.json"
        //url : "/search/search.html"
    }
    $scope.gridClass = 'grid clearfix four paginated';//'grid clearfix four paginated';//grid show_all_on_hover no_info four
    if (sjApp.ismobile) {
        $scope.hidden = true;
        $scope.gridClass = 'grid three';
    }
    var photoEl = $('#px.photos_index');
    if (photoEl.length) {
        photoEl.removeClass('clearfix four paginated');
        photoEl.addClass('three');
    }
    var category = $.getUrlParam('category');
    $http.get(community.url, {
        params: {
            type: 'json',
            category: category || 0,
            dc: new Date().getTime()
        }
    }).success(function (response) {
        var result = response;
        $scope.photos = result.list;

        setTimeout(function () {
            return;
            var fy;
            if (sjApp.ismobile) {
                fy = $('<a class="" id="more"><div class="loading"></div><div class="more">More</div></a>');
            } else {
                fy = $('<div class="pagination pager box"><ul><li><a class="previous_page disabled" href="javascript:void(false)" rel="previous">上一页</a><div class="pages">	<em class="current">1</em><a href="javascript:void(false)">2</a><a href="javascript:void(false)">3</a><a href="javascript:void(false)">4</a><a href="javascript:void(false)">5</a><span class="gap">…</span><a href="javascript:void(false)">11</a><a href="javascript:void(false)">12</a><a href="javascript:void(false)">13</a><a href="javascript:void(false)">14</a><a href="javascript:void(false)">15</a></div> <a class="next_page" href="javascript:void(false)" rel="next">下一页</a></li></ul></div>')
            }
            var dd = $('.photos');
            dd.after(fy);

            '<a data-provider="qzone" class="action share qzone"></a><a data-provider="weibo" class="action share weibo"></a><a data-provider="huaban" class="action share huaban"></a>'
            return;
        }, 1000)
    });
});
/*发现页end*/

/*首页begin*/
navbarApp.controller('homeController', function ($scope, $http) {
    $scope.clsJson = ['one', 'two', 'three', 'four'];
    $scope.dd = function () {
        return '';
    }
    $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
//			$(window).lazyLoadXT();
        //下面是在table render完成后执行的js
        var options = $.lazyLoadXT, bgAttr = options.bgAttr || 'data-bg';
        options.selector += ',[' + bgAttr + ']';
        $(window).on('lazyshow', function (e) {
            var $this = $(e.target);
            $this.css('background-image', "url('" + $this.attr(bgAttr) + "')").removeAttr(bgAttr);
        });
        $(window).lazyLoadXT();
    });
    $scope.templateFilter = function () {
        var cls = '';
        switch (this.pt.template) {
            case 1:
                cls = 'one';
                break;
            case 2:
                cls = 'three';
                break;
            case 3:
                cls = 'three';
                break;
            case 4:
                cls = 'two';
                break;
            default:
                break;
        }
        return cls;
    }
    var community = {
        url: "json/home.json"
        //url : "/search/flow.html"
    }
    $http.get(community.url, {
        params: {
            type: 'json',
            category: 0,
            dc: new Date().getTime()
        }
    }).success(function (response) {
        var result = response;
        $scope.photos = result.result;
    });
});
navbarApp.directive('onFinishRenderFilters', function ($timeout) {
    return {
        restrict: 'A',
        link: function (scope, element, attr) {
            if (scope.$last === true) {
                $timeout(function () {
                    scope.$emit('ngRepeatFinished');
                });
            } else {
                $timeout(function () {
                    scope.$emit('finished');
                });
            }
        }
    };
});
/*首页end*/
/*图片祥情页begin*/
navbarApp.controller('photo_detail_controller', function ($scope, $http) {
    $http.get('/photo-details/json/photo_details.json', {
        params: {
            type: 'json',
            dc: new Date().getTime()
        }
    }).success(function (response) {
        $scope.result = response;

    });
});
/*图片祥情页end*/
/*大赛新建begin*/
navbarApp.controller('home_photo_Controller', function ($scope, $http) {
    $scope.photos = []
});
/*图片祥情页end*/